package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmFollowupService;
import com.mdd.admin.validate.ClientCrmFollowupCreateValidate;
import com.mdd.admin.validate.ClientCrmFollowupUpdateValidate;
import com.mdd.admin.validate.ClientCrmFollowupSearchValidate;
import com.mdd.admin.vo.ClientCrmFollowupListedVo;
import com.mdd.admin.vo.ClientCrmFollowupDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmFollowup;
import com.mdd.common.mapper.ClientCrmFollowupMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 跟进记录实现类
 * @author 洁洁
 */
@Service
public class ClientCrmFollowupServiceImpl implements IClientCrmFollowupService {
        
    @Resource
    ClientCrmFollowupMapper clientCrmFollowupMapper;

    /**
     * 跟进记录列表
     *
     * @author 洁洁
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmFollowupListedVo>
     */
    @Override
    public PageResult<ClientCrmFollowupListedVo> list(PageValidate pageValidate, ClientCrmFollowupSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmFollowup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmFollowupMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "between:followUpTime@follow_up_time:str",
            "=:module:str",
            "=:type:int",
            "=:userId@user_id:int",
        });

        IPage<ClientCrmFollowup> iPage = clientCrmFollowupMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmFollowupListedVo> list = new LinkedList<>();
        for(ClientCrmFollowup item : iPage.getRecords()) {
            ClientCrmFollowupListedVo vo = new ClientCrmFollowupListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setFollowUpTime(TimeUtils.timestampToDate(item.getFollowUpTime()));
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 跟进记录详情
     *
     * @author 洁洁
     * @param id 主键参数
     * @return ClientCrmFollowup
     */
    @Override
    public ClientCrmFollowupDetailVo detail(Integer id) {
        ClientCrmFollowup model = clientCrmFollowupMapper.selectOne(
                new QueryWrapper<ClientCrmFollowup>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmFollowupDetailVo vo = new ClientCrmFollowupDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 跟进记录新增
     *
     * @author 洁洁
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmFollowupCreateValidate createValidate) {
        ClientCrmFollowup model = new ClientCrmFollowup();
        model.setName(createValidate.getName());
        model.setFollowUpTime(createValidate.getFollowUpTime());
        model.setModule(createValidate.getModule());
        model.setType(createValidate.getType());
        model.setStatus(createValidate.getStatus());
        model.setFollowUpId(createValidate.getFollowUpId());
        model.setLinkmanId(createValidate.getLinkmanId());
        model.setPhone(createValidate.getPhone());
        model.setFileId(createValidate.getFileId());
        model.setFollowUpContent(createValidate.getFollowUpContent());
        model.setNextFollowUpTime(createValidate.getNextFollowUpTime());
        model.setUserId(LikeAdminThreadLocal.getAdminId());
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmFollowupMapper.insert(model);
    }

    /**
     * 跟进记录编辑
     *
     * @author 洁洁
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmFollowupUpdateValidate updateValidate) {
        ClientCrmFollowup model = clientCrmFollowupMapper.selectOne(
                new QueryWrapper<ClientCrmFollowup>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setFollowUpTime(updateValidate.getFollowUpTime());
        model.setModule(updateValidate.getModule());
        model.setType(updateValidate.getType());
        model.setStatus(updateValidate.getStatus());
        model.setFollowUpId(updateValidate.getFollowUpId());
        model.setLinkmanId(updateValidate.getLinkmanId());
        model.setPhone(updateValidate.getPhone());
        model.setFileId(updateValidate.getFileId());
        model.setFollowUpContent(updateValidate.getFollowUpContent());
        model.setNextFollowUpTime(updateValidate.getNextFollowUpTime());
        clientCrmFollowupMapper.updateById(model);
    }

    /**
     * 跟进记录删除
     *
     * @author 洁洁
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmFollowup model = clientCrmFollowupMapper.selectOne(
                new QueryWrapper<ClientCrmFollowup>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmFollowupMapper.updateById(model);
    }

}
